package com.atguigu.mr.outputformat;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

/**
 * @description: xxx
 * @time: 20191229 11:12
 * @author: baojinlong
 **/
public class FilterDriver {
    public static void main(String[] args) throws Exception {
        // 输入输出路径需要根据自己电脑上实际的输入输出路径设置
        String[] testPath = new String[]{"D:/home/test/big-data/input56", "D:/home/test/big-data/output-log233"};
        // 1 获取job对象
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);

        // 2 设置jar包存储位置、关联自定义的mapper和reducer
        job.setJarByClass(FilterDriver.class);
        job.setMapperClass(FilterMapper.class);
        job.setReducerClass(FilterReducer.class);

        // 3 设置map输出端的kv类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(NullWritable.class);

        // 4 设置最终输出端的kv类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);

        // 将自定义的输出格式组件设置到job中
        job.setOutputFormatClass(FilterOutputFormat.class);
        // 5 设置输入输出路径
        FileInputFormat.setInputPaths(job, new Path(testPath[0]));
        // 仍然设置fileOutputFormat,因为其要输出一个_SUCCESS文件,所以需要指定一个输出目录
        FileOutputFormat.setOutputPath(job, new Path(testPath[1]));
        // 6 提交job
        boolean result = job.waitForCompletion(true);
        System.exit(result ? 0 : 1);
    }
}
